//
//  HomeViewController.m
//  随时随地测试项目
//
//  Created by IOS_DEVELOPER on 2017/6/1.
//  Copyright © 2017年 陈志凌. All rights reserved.
//

#import "ZJHomeViewController.h"
#import "ZJHomeModel.h"
#import "ZJHomeCollectionCell.h"
#import "ZJUserModel.h"

#import "ZJMineController.h"
#import "ZJSignUpInAdvanceController.h"
#import "ZJExtensionAgentController.h"
#import "ZJOnlinePayHouseController.h"
#import "ZJProjectInformationVC.h"
#import "ZJActivityCenterVC.h"
#import "ZJBreakAccountVC.h"
#import "ZJTaskAccountVC.h"
#import "ZJHongBaoVC.h"
#import "ZJSuccessfulEnrollmentController.h"
#import "ZJTuiGuangShuoMingVC.h"
#import "ZJRoomModelVC.h"
#import "ZJZXAndWYVCView.h"

#import "UINavigationBar+Awesome.h"
#import "UIViewController+KNSemiModal.h"

#import "ZJOrientationModel.h"
#import "ZJHouse_layoutModel.h"
#import "ZJArea_circleMdel.h"
#import "ZJExtensionVC.h"
#import "ZJChangeProjectVC.h"

#import "ZJDepositPaymentController.h"
#define NAVBAR_CHANGE_POINT 50

//房屋面积全局数组
NSArray *area_circleArr;
//房屋类型全局数组
NSArray *house_layoutArr;
//房屋朝向全局数组
NSArray *orientationArr;
//交钱协议
NSString *agreement_zhejia100;
//推广员协议
NSString *agreement_promoter;
//红包信息
NSDictionary *lucky_package;
//最新版本信息
NSDictionary *version;

@interface ZJHomeViewController ()<UIScrollViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,ZJChangeProjectVCDelegate>{
    UIImageView *navBarHairlineImageView;
    UIImageView *tabBarHairlineImageView;
    NSInteger currentIndex;
    NSInteger once;
}

@property (weak, nonatomic) IBOutlet UIView *topView;
//头部视图底部
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UIImageView *img;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollview;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property (strong, nonatomic) ZJHomeModel * model;
@property (strong, nonatomic) ZJProjectDetailModel * proModel;
@property (strong, nonatomic) ZJUserModel * userModel;
@property (weak, nonatomic) IBOutlet UILabel *titleName;
@property (weak, nonatomic) IBOutlet UIButton *leftBtn;
@property (weak, nonatomic) IBOutlet UIButton *rightBtn;
/**<模型数组>*/
@property (nonatomic,strong) NSArray *modelArr;
@property (strong, nonatomic)UIAlertController *alertController;
@end
@implementation houseType

@end
@implementation ZJHomeViewController

-(ZJHomeModel *)model{
    if (!_model) {
        _model = [[ZJHomeModel alloc]init];
        //测试数据
        _model.imagePath =  @"zwt_b";
    }
    return _model;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
//    [self.mm_drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    
//    UIColor * color = [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1];
//    [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    self.navigationController.navigationBar.translucent = YES;
    navBarHairlineImageView.hidden = YES;
    tabBarHairlineImageView.hidden = YES;
    self.userModel = GET_USERINFO;
    self.proModel = GET_PROJECTINFO;
    self.navigationController.navigationBarHidden = YES;

}

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.bottomView.backgroundColor = [UIColor redColor];
//    if (SCREEN_HEIGHT <= 568) {
        self.topView.sd_layout.leftSpaceToView(self.view, 0).topSpaceToView(self.view,0).rightSpaceToView(self.view, 0).heightIs(SCREEN_HEIGHT*0.4);
        self.img.sd_layout.leftSpaceToView(self.topView, 0).topSpaceToView(self.topView, 0).rightSpaceToView(self.topView, 0).bottomSpaceToView(self.topView, 0);
        self.bottomView.sd_layout.leftSpaceToView(self.topView, 0).bottomSpaceToView(self.topView, 8).rightSpaceToView(self.topView, 0).heightIs(70);
       self.collectionView.sd_layout.leftSpaceToView(self.view, 0).topSpaceToView(self.topView, 0).rightSpaceToView(self.view, 0).heightIs(SCREEN_HEIGHT*0.6);
        [self.view setupAutoHeightWithBottomViewsArray:@[self.topView,self.collectionView] bottomMargin:0];
        
//    }
    currentIndex = 0;

    [self setupCollectionLayout];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mainUpData) name:kHomeDataChange object:nil];
//    [self mainUpData];
    // 隐藏底部黑线
    navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
    tabBarHairlineImageView = [self findHairlineImageViewUnder:self.tabBarController.tabBar];
    if (allConstMemberid && allConstMD5id) {
        [self sendRequest];
    }
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:YES];

}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kHomeDataChange object:nil];

}
#pragma mark - 页面控件设置
-(void)setupScrollView{
    self.scrollview.bounces = NO;
    self.scrollview.pagingEnabled = YES;
    
    float btnWidth = self.scrollview.frame.size.width;
    float btnHeight = self.scrollview.frame.size.height - 5;
    
    self.scrollview.contentSize = CGSizeMake(self.modelArr.count * self.scrollview.frame.size.width , 0);
    if (self.modelArr.count <= 1) {
        self.leftBtn.hidden = YES;
        self.rightBtn.hidden = YES;
    }else{
        self.leftBtn.hidden = NO;
        self.rightBtn.hidden = NO;
    }
    NSArray *subViews = [self.scrollview subviews];
    
    if([subViews count] != 0) {
        
        [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
    }
    for (int i = 0; i < self.modelArr.count; i++) {
        houseType * model = self.modelArr[i];
        
        if (i == 0) {
            self.titleName.text = model.name;
        }
        
        UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(i*btnWidth, 0, btnWidth, btnHeight)];
        [btn setTitle:model.content forState:0];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [btn setTintColor:[UIColor whiteColor]];
        btn.titleLabel.font = [UIFont systemFontOfSize:[Utils iphone5AutoScaleW:12.0]];
        btn.tag = i;
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:(UIControlEventTouchUpInside)];
        [self.scrollview addSubview:btn];
    }
    
    UIButton* menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [menuBtn setImage:[UIImage imageNamed:@"nav_icon_ch"]
             forState:UIControlStateNormal];
    [menuBtn sizeToFit];
    [menuBtn addTarget:self
                action:@selector(menuBtn)
      forControlEvents:UIControlEventTouchUpInside];
    
    menuBtn.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    menuBtn.frame = CGRectMake(0, 0, 20, 20);
    self.navigationItem.rightBarButtonItem =
    [[UIBarButtonItem alloc] initWithCustomView:menuBtn];
    
    UIButton* backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [backButton setImage:[UIImage imageNamed:@"nav_icon_sys"]
                forState:UIControlStateNormal];
    backButton.frame = CGRectMake(0, 0, 20, 20);
    [backButton addTarget:self
                   action:@selector(backAction)
         forControlEvents:UIControlEventTouchUpInside];
    
    backButton.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    self.navigationItem.leftBarButtonItem =
    [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    
    //主题设置
    self.titleName.textColor = [UIColor whiteColor];

    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:allConstNavImgName] forBarMetrics:UIBarMetricsDefault];
}

-(void)menuBtn{
     [self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];
}
-(void)backAction{
    HMScannerController *scanner = [HMScannerController scannerWithCardName:@"" avatar:nil completion:^(NSString *stringValue) {
        
        NSArray *arr = [stringValue componentsSeparatedByString:@"="];
        if (arr.count < 2) {
            return;
        }
        [HUD showMsg:@"扫描结束"];
        
    }];
    
    [scanner setTitleColor:[UIColor whiteColor] tintColor:GREEN_COLOR];
    
    [self showDetailViewController:scanner sender:nil];
}

-(void)setupCollectionLayout{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    //设置布局方向为垂直流布局
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    //设置每个item的大小
    layout.itemSize = CGSizeMake(SCREEN_WIDTH/3, self.collectionView.height/3);
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    
    self.collectionView.collectionViewLayout = layout;
}

-(void)clickBtn:(UIButton*)button{
    if ([self.titleName.text isEqualToString:@"主推户型"]) {
        ZJRoomModelVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJRoomModelVC"];
        [self.navigationController pushViewController:vc animated:YES];
    }

}
#pragma mark - ******************请求数据
-(void)sendRequest{
    NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
    NSDictionary *parameter = [LRNetwork requestParameter:@{
                                                            @"e_type":@"1",
                                                            @"e_width":@(SCREEN_WIDTH),
                                                            @"e_height":@(SCREEN_HEIGHT),
                                                            @"os_version":phoneVersion,
                                                            @"app_version":appVersion
                                                            }];
    
    NSString *url = [LRNetwork requestURL:@"cgi/open.php"];
    // 登录
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        
        allConstMemberid = returnValue[@"user_id"];
        allConstMD5id = [Utils md5:[NSString stringWithFormat:@"%@_%@|%@|%@",allConstApp_id,allConstProjectid,returnValue[@"user_id"],returnValue[@"verify"]]];
        [USER_DEFAULTS setObject:returnValue[@"user_id"] forKey:@"user_id"];
        [USER_DEFAULTS setObject:allConstMD5id forKey:@"verify"];
        [USER_DEFAULTS setObject:returnValue[@"verify"] forKey:@"verify1"];
        [USER_DEFAULTS synchronize];
        NSLog(@"用户数据更新成功");
        [self mainUpData];
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        NSLog(@"error:%@",errorMessage);
    } withFailureBlock:^{
        
    }];
    
}
#pragma mark - 故事版按钮方法
- (IBAction)storyBtn:(UIButton *)sender {
    switch (sender.tag) {
        case 1001:
            currentIndex --;
            if (![self moveScrollView:currentIndex]) {

                return;
            }
            break;//左滑
            
        case 1002:
            currentIndex ++;
            if (![self moveScrollView:currentIndex]) {

                return;
            }
            break;//右滑
            
        case 1003:{
            HMScannerController *scanner = [HMScannerController scannerWithCardName:@"" avatar:nil completion:^(NSString *stringValue) {
                
                NSArray *arr = [stringValue componentsSeparatedByString:@"="];
                if (arr.count < 2) {
                    return;
                }
                [HUD showMsg:@"扫描结束"];
                
            }];
            
            [scanner setTitleColor:[UIColor whiteColor] tintColor:[Utils colorWithHexString:allConstHexColor]];
            
            [self showDetailViewController:scanner sender:nil];
        
        }
            break;//扫码
            
        case 1004:{

//             [self.mm_drawerController toggleDrawerSide:MMDrawerSideRight animated:YES completion:nil];
            ZJChangeProjectVC * vc = [[ZJChangeProjectVC alloc]init];
            vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            vc.delegate = self;
            [self presentViewController:vc animated:YES completion:nil];
          
        }
            break;//菜单
        default:
            break;
    }
}
#pragma mark - 切换项目代理
-(void)changeProject{
    
    [self mainUpData];
#warning 项目改动
    if ([allConstProjectid integerValue] == 7) {
        self.alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"切换至'南滨上院'" preferredStyle:UIAlertControllerStyleAlert];
        
    }else{
        self.alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"切换至'星光上院'" preferredStyle:UIAlertControllerStyleAlert];
    }
    
    [self presentViewController:_alertController animated:YES completion:nil];
    
    [self performSelector:@selector(hide) withObject:self afterDelay:1.0];
}

-(BOOL)moveScrollView:(NSInteger)index{
    if (index < 0 ) {
        currentIndex = 0;
        return NO;
    }else if (index >= self.modelArr.count){
        currentIndex = self.modelArr.count - 1;
        return NO;
    }
    else{
        houseType * model = self.modelArr[currentIndex];
        self.titleName.text = model.name;
        [self.scrollview setContentOffset:CGPointMake(currentIndex * self.scrollview.frame.size.width, 0) animated:YES];
        return YES;
    }
}
#pragma mark -UIScrollView代理

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if ([scrollView isKindOfClass:[UITableView class]]) {
        return;
    }
    //计算页数
    int page = scrollView.contentOffset.x / scrollView.bounds.size.width;
    if (page >= self.modelArr.count) {
        page = (int)self.modelArr.count - 1;
    }
    currentIndex = page;
    if (page >= 0) {
        houseType * model = self.modelArr[page];
        self.titleName.text = model.name;
    }

}
#pragma mark -UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.model.homeImage.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZJHomeCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeCollectionCell" forIndexPath:indexPath];
    cell.imgview.image = [UIImage imageWithContentsOfFile:self.model.homeImage[indexPath.row]];
    cell.topText.text = self.model.homeTopText[indexPath.row];
    
//    if (indexPath.row %2 !=0) {
//        cell.backgroundColor = RGB(249.0, 249.0, 249.0);
//    }
    
    cell.smallText.text = self.model.homeTopSmallText[indexPath.row];
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
 
    switch (indexPath.row) {
            //楼盘综述
        case 0:
        {
            ZJProjectInformationVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJProjectInformationVC"];
            vc.prModel = self.proModel;
            [self.navigationController pushViewController:vc animated:YES];
//            ZJTuiGuangShuoMingVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJTuiGuangShuoMingVC"];
//            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
            //任务
        case 1:
        {
            ZJTaskAccountVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJTaskAccountVC"];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
            //活动
        case 2:
        {
            ZJActivityCenterVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJActivityCenterVC"];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            //我的账户
        case 3:
        {
            
            ZJMineController *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJMineController"];
            [self.navigationController pushViewController:vc animated:YES];

        }
            break;
            //红包
        case 4:
        {
            if ([CLLocationManager locationServicesEnabled] &&[ CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
                
                self.alertController = [UIAlertController alertControllerWithTitle:@"打开定位开关" message:@"请到“设置 - 后台 - ”打开定位服务" preferredStyle:UIAlertControllerStyleAlert];
                
                [self presentViewController:_alertController animated:YES completion:nil];
                
                [self performSelector:@selector(hide) withObject:self afterDelay:2.5];
                
                
            }else{
            ZJHongBaoVC * vc = [[ZJHongBaoVC alloc]init];
            [self presentViewController:vc animated:YES completion:nil];
                
            }
        }
            break;
            //优惠管理
        case 5:
        {
            ZJBreakAccountVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJBreakAccountVC"];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            
            break;
            //提前报名
        case 6:
        {
            if ([self.userModel.is_sincere isEqualToString:@"1"]) {
                
                if ([self.userModel.deposit_return integerValue] !=1) {
                    
                    if ([self.userModel.deposit_can_return integerValue] ==1) {
                        ZJSuccessfulEnrollmentController *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJSuccessfulEnrollmentController"];
                        vc.deposit_return_type = @"退诚意金";
                        vc.isBackRoot = YES;
                        [self.navigationController pushViewController:vc animated:YES];
                        
                    }else{
                        ZJSuccessfulEnrollmentController *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJSuccessfulEnrollmentController"];
                        vc.isBackRoot = YES;
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                    
                }else{
                
                ZJSuccessfulEnrollmentController *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJSuccessfulEnrollmentController"];
                    vc.deposit_return_type = @"已退诚意金";
                    vc.isBackRoot = YES;
                [self.navigationController pushViewController:vc animated:YES];
                    
                }
                
            }else{
                
            ZJSignUpInAdvanceController *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJSignUpInAdvanceController"];
            [self.navigationController pushViewController:vc animated:YES];
                
            }
        }
            break;
            //在线购房
        case 7:
        {
            
            if ([self.proModel.house_added integerValue] != 1) {
                self.alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"此楼盘暂时未开通网上售房服务" preferredStyle:UIAlertControllerStyleAlert];
                
                [self presentViewController:_alertController animated:YES completion:nil];
                
                [self performSelector:@selector(hide) withObject:self afterDelay:2.5];
                
            }else{
                
                once = 0;
                NSString * build = version[@"build"];
                if ([appBuild integerValue] < [build integerValue] && version[@"build"]) { // 版本号相同：这次打开和上次打开的是同一个版本
                    
                    static dispatch_once_t justDoOnce;
                    dispatch_once(&justDoOnce, ^{
                        once = 1;
                        ZJTuiGuangShuoMingVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJTuiGuangShuoMingVC"];
                        vc.titleStr = @"开始购房";
                        [self.navigationController pushViewController:vc animated:YES];
                        
                    });
                    
                    if (once != 1) {
                        ZJOnlinePayHouseController *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJOnlinePayHouseController"];
                        vc.prModel = self.proModel;
                        [self.navigationController pushViewController:vc animated:YES];
                    }
                    
                }else{
                
                    ZJOnlinePayHouseController *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJOnlinePayHouseController"];
                    vc.prModel = self.proModel;
                    [self.navigationController pushViewController:vc animated:YES];
                    
                }
                
            }
        }
            break;
            //推广员
        case 8:
        {
            
            //是否推广员
            if ([self.userModel.is_promoter integerValue] == 1) {
                
                ZJExtensionAgentController *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJExtensionAgentController"];
                [self.navigationController pushViewController:vc animated:YES];
                
            }else{
            
                if ([self.proModel.promoter_only_sincere integerValue] == 1) {
                    
                    if ([self.userModel.is_sincere integerValue] == 1) {
                        
                        ZJTuiGuangShuoMingVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJTuiGuangShuoMingVC"];
                        vc.titleStr = @"成为推广员";
                        [self.navigationController pushViewController:vc animated:YES];
                        
                    }else{
                    
                        self.alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先完成提前报名~" preferredStyle:UIAlertControllerStyleAlert];
                        
                        [self presentViewController:_alertController animated:YES completion:nil];
                        
                        [self performSelector:@selector(hide) withObject:self afterDelay:2.5];
                    }
                }else{
                
                    ZJTuiGuangShuoMingVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJTuiGuangShuoMingVC"];
                    [self.navigationController pushViewController:vc animated:YES];
                }
            }

        }
            break;
            //装修服务
        case 9:
        {
            ZJZXAndWYVCView *vc = [[ZJZXAndWYVCView alloc]init];
            vc.titleStr = @"装修服务";
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
            //物业服务
        case 10:
        {
            ZJZXAndWYVCView *vc = [[ZJZXAndWYVCView alloc]init];
            vc.titleStr = @"物业服务";
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        default:
            break;
    }

}
-(void)hide{
    [self.alertController dismissViewControllerAnimated:YES completion:nil];
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

-(void)mainUpData{
    self.img.image = [UIImage imageWithContentsOfFile:[Utils imgPath:@"sy_pic_ban@3x.png"]];
    [HUD showLoading:@"加载数据中..."];
    NSDictionary *parameter = [LRNetwork requestParameter:@{}];
    NSString *url = [LRNetwork requestURL:@"cgi/main.php"];
    
    [LRNetwork netRequestPOSTWithRequestURL:url withParameter:parameter withReturnValeuBlock:^(id returnValue) {
        
        self.modelArr = [houseType mj_objectArrayWithKeyValuesArray:returnValue[@"slide"]];
        
        house_layoutArr = [ZJHouse_layoutModel mj_objectArrayWithKeyValuesArray:returnValue[@"house_layout"]];
        area_circleArr = [ZJArea_circleMdel mj_objectArrayWithKeyValuesArray:returnValue[@"area_circle"]];
        orientationArr = [ZJOrientationModel mj_objectArrayWithKeyValuesArray:returnValue[@"orientation"]];
        
        [HUD hide];
        
        self.proModel = [ZJProjectDetailModel mj_objectWithKeyValues:returnValue[@"project"]];
        lucky_package = returnValue[@"lucky_package"];
        version = returnValue[@"version"];
        allConstAuthenticated = returnValue[@"user"][@"authenticated"];
        agreement_zhejia100 = self.proModel.agreement_zhejia100;
        agreement_promoter = self.proModel.agreement_promoter;
        SYN_PROJECT(self.proModel);
        
        self.userModel = [ZJUserModel mj_objectWithKeyValues:returnValue[@"user"]];
        SYN_USER( self.userModel);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"upLeftViewData" object:@{@"model":self.userModel}];
        
        if ([allOpen_promoter integerValue] == 1) {
            allOpen_promoter = @"0";
            ZJExtensionVC *vc = [[UIStoryboard storyboardWithName:@"Home" bundle:nil] instantiateViewControllerWithIdentifier:@"ZJExtensionVC"];
            vc.type = YES;
//            vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            [self presentViewController:vc animated:YES completion:nil];
        }
        NSString * build = version[@"build"];
        if ([appBuild integerValue] < [build integerValue] && version[@"build"]) { // 版本号相同：这次打开和上次打开的是同一个版本
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                [[[UIAlertView alloc] initWithTitle:@"技术君的悄悄话" message:version[@"content"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"前往更新", nil] show];
            });
        }
        _model.homeImage = @[[Utils imgPath:@"sy_icon_lp@3x.png"],
                             [Utils imgPath:@"sy_icon_rw@3x.png"],
                             [Utils imgPath:@"sy_icon_hd@3x.png"],
                             [Utils imgPath:@"sy_icon_zh@3x.png"],
                             [Utils imgPath:@"sy_icon_hb@3x.png"],
                             [Utils imgPath:@"sy_icon_zxyh@3x.png"],
                             [Utils imgPath:@"sy_icon_bm@3x.png"],
                             [Utils imgPath:@"sy_icon_zxgf@3x.png"],
                             [Utils imgPath:@"sy_icon_tgy@3x.png"]];
        
        _model.homeTopText = @[@"楼盘信息",
                               @"任务",
                               @"活动",
                               @"个人中心",
                               @"抢红包",
                               @"专享优惠",
                               @"提前报名",
                               @"在线购房",
                               @"推广员"];
        _model.homeTopSmallText = @[@"building",
                                    @"Mission",
                                    @"Activity",
                                    @"Account",
                                    @"Red Wars",
                                    @"privilege",
                                    @"Registration",
                                    @"Online Purchase",
                                    @"Agent"];
#warning 项目改动
        if ([allConstProjectid integerValue] == 7) {
                    _model.homeImage = @[[Utils imgPath:@"sy_icon_lp@3x.png"],
                                         [Utils imgPath:@"sy_icon_rw@3x.png"],
                                         [Utils imgPath:@"sy_icon_hd@3x.png"],
                                         [Utils imgPath:@"sy_icon_zh@3x.png"],
                                         [Utils imgPath:@"sy_icon_hb@3x.png"],
                                         [Utils imgPath:@"sy_icon_zxyh@3x.png"],
                                         [Utils imgPath:@"sy_icon_bm@3x.png"],
                                         [Utils imgPath:@"sy_icon_zxgf@3x.png"],
                                         [Utils imgPath:@"sy_icon_tgy@3x.png"],
                                         [Utils imgPath:@"sy_icon_zzx@3x.png"],
                                         [Utils imgPath:@"sy_icon_wy@3x.png"]];
            
            _model.homeTopText = @[@"楼盘信息",
                                   @"任务",
                                   @"活动",
                                   @"个人中心",
                                   @"抢红包",
                                   @"专享优惠",
                                   @"提前报名",
                                   @"在线购房",
                                   @"推广员",
                                   @"装修服务",
                                   @"物业服务"];
            
            _model.homeTopSmallText = @[@"Building",
                                        @"Mission",
                                        @"Activity",
                                        @"Account",
                                        @"Red Wars",
                                        @"Privilege",
                                        @"Registration",
                                        @"Online Purchase",
                                        @"Agent",
                                        @"Fitment",
                                        @"Realty service"];
        }
        
        UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(0, self.topView.height - 66, SCREEN_WIDTH, 70)];
        imgv.image = [UIImage imageWithContentsOfFile:[Utils imgPath:@"sy_pic_bg@3x.png"]];
        imgv.layer.shadowColor = [Utils colorWithHexString:allConstHexColor].CGColor;;
        imgv.layer.shadowOpacity = 0.8f;
        imgv.layer.shadowOffset = CGSizeMake(0,5);
        if (self.modelArr.count > 0) {
            [self.img addSubview:imgv];
        }else{
            self.titleName.text = @"";
            NSArray *subViews = [self.img subviews];
            [subViews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        }
        
        [self.collectionView reloadData];
        [self setupScrollView];
    } withErrorCodeBlock:^(NSInteger errorCode, NSString *errorMessage) {
        [HUD hide];
        NSLog(@"error:%@",errorMessage);
    } withFailureBlock:^{
    }];
    [HUD hide];
}

#pragma mark - alertview
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/us/app/qingjiaoshenghuo-shangjiaduan/id1211112078?l=zh&ls=1&mt=8"]];
    }
    
}
@end
